import pool from '../config/database.js';

export const StaffModel = {
  async getAllStaff() {
    try {
      const [rows] = await pool.query('SELECT * FROM staff ORDER BY created_at DESC');
      return rows.map(row => ({
        id: row.id,
        name: row.name,
        position: row.position,
        department: row.department,
        phone: row.phone,
        status: row.status
      }));
    } catch (error) {
      throw new Error('Failed to fetch staff');
    }
  },

  async createStaff(staff) {
    try {
      const [result] = await pool.query(
        'INSERT INTO staff (name, position, department, phone, status) VALUES (?, ?, ?, ?, ?)',
        [staff.name, staff.position, staff.department, staff.phone, staff.status]
      );
      return result.insertId;
    } catch (error) {
      throw new Error('Failed to create staff member');
    }
  },

  async updateStaff(id, staff) {
    try {
      const [result] = await pool.query(
        'UPDATE staff SET name = ?, position = ?, department = ?, phone = ?, status = ? WHERE id = ?',
        [staff.name, staff.position, staff.department, staff.phone, staff.status, id]
      );
      return result.affectedRows > 0;
    } catch (error) {
      throw new Error('Failed to update staff member');
    }
  },

  async deleteStaff(id) {
    try {
      const [result] = await pool.query('DELETE FROM staff WHERE id = ?', [id]);
      return result.affectedRows > 0;
    } catch (error) {
      throw new Error('Failed to delete staff member');
    }
  }
};